{% extends "layout.html" %}
{% block __data_browser__custom_external_block %}
  <script type="text/javascript">
    $(function () {
      $("#procedure").val({{ work_command.procedure.id }});
      {% if work_command.status == 2 and request.args.get("url")%}
        $("button").click(function () {
          location.href = "{{ request.args.get("url") }}"
        });
      {% endif %}
      var departments = {{ department_list|tojson|safe }};

      function update_procedure() {
        $("#procedure_id").empty();
        var selected_department_id = $("#department_id").val();
        for (i = 0; i < departments.length; ++i) {
          if (departments[i].id == selected_department_id) {
            var procedure_list = departments[i].procedure_list;
            for (j = 0; j < procedure_list.length; ++j) {
              $("#procedure_id").append("<option value=" + procedure_list[j].id + ">" + procedure_list[j].name + "</option>");
            }
          }
        }
        $("select").select2({
          width: "element",
          placeholder: "请选择",
          matcher: function (term, text) {
            return pinyin.getCamelChars(text).toUpperCase().indexOf(term.toUpperCase()) >= 0;
          }
        });
      }

      update_procedure();
      $("#department_id").change(update_procedure);
    });
  </script>
{% endblock %}
{% block body %}
  <div class="container">
    <legend>排产</legend>
    <form class="form-horizontal" method="POST">
      <div class="form-group">
        <label class="control-label col-lg-2">数量(单位:{{ work_command.unit }})</label>

        <div class="col-lg-3">
          <p class="form-control-static">{{ work_command.org_cnt }}</p>
        </div>
      </div>
      <div class="form-group">
        <label for="department_id" class="control-label col-lg-2">车间</label>

        <div class="col-lg-3">
          <select name="department_id" id="department_id" required='required' class="form-control">
            {% for department in department_list %}
              <option value="{{ department.id }}"
                  {% if department.id == work_command.default_department.id %}
                      selected="selected"
                  {% endif %} >{{ department.name }}</option>
            {% endfor %}
          </select>
          <span class="help-block">工单<em>{{ work_command.id }}</em>的默认车间是<em>{{ work_command.default_department.name }}</em></span>
        </div>
      </div>
      <div class="form-group">
        <label for="tech_req" class="control-label col-lg-2">技术要求</label>

        <div class="col-lg-3">
          <input size="10" name="tech_req" id="tech_req" type="text" value="{{ work_command.tech_req }}" class="form-control"/>
        </div>
      </div>
      <div class="form-group">
        <label for="urgent-input" class="control-label col-lg-2">加急</label>

        <div class="col-lg-3">
          <div class="checkbox">
            <input name="urgent" id="urgent-input" type="checkbox"
                {% if work_command.urgent %}
                   checked="checkbox"
                {% endif %} />
          </div>
        </div>
      </div>
      <div class="form-group">
        <label for="procedure_id" class="control-label col-lg-2">工序</label>

        <div class="col-lg-3">
          <select size="1" name="procedure_id" id="procedure_id" required='required' class="form-control">
          </select>
        </div>
      </div>
      <input type="hidden" name="id" value="{{ work_command.id }}"/>
      <input type="hidden" name="url" value="{{ request.args.get("url") or url_for("manufacture.work_command_list") }}"/>

      <div class="form-group">
        <div class="col-lg-3 col-lg-offset-2">
          <button class="btn btn-primary btn-lg">
            <i class="fa fa-wrench fa-fw"></i>排产
          </button>
          <a class="btn btn-lg btn-default" href="{{ request.args.get("url") or url_for("manufacture.work_command_list") }}">
            <i class="fa fa-step-backward fa-fw"></i>返回
          </a>
        </div>
      </div>
    </form>
  </div>

{% endblock %}
